
var gs3 = require('gs3');
var Discussions_Abstract = gs3.Modules.Messaging.Discussion_Management;

var db_config = require('./db_config');

var announcementTable = {
    tableName: "tbannouncement",      //  required
    primary: "id",                  //  required
    userId: "user_id",              //  required
    lastModified: "date"   //  not required, used here in the implementation
                                    //  module only (see updatePost method)
};

var commentTable = {
    tableName: "tbcomment",
    primary: "id",
    userId: "user_id",
    topicId: "announcement_id"
};

//  Instance Scoped (relative to Discussions_Abstract) Inheritance
var Discussions_Impl = new Discussions_Abstract(db_config, announcementTable, commentTable);

// TODO: create Discussions_Impl override methods below as needed

/** 
 * @ApplicationScoped
 * @Override
 * @param {SQLObject} postObject
 * @param {function} callback
 * @returns {undefined}
 */
Discussions_Impl.updatePost = function(postObject, callback) {
    var db = Discussions_Impl.getDatabase();
    db.connect();

    var condition = "WHERE " + announcementTable.userId + "='" + postObject.fields[announcementTable.userId] + "'";    
    condition += " AND " + announcementTable.primary + "='" + postObject.fields[announcementTable.primary] + "'";
    
    //  remove the primary key so that it wont be updated
    delete postObject[announcementTable.primary];
    
    //  override code, add current time to the last_modified column everytime
    //  an update is commited.
    postObject.Last_Modified = "NOW()";    

    db.updateByCondition(postObject, condition, function(err, data) {
        if (err) {
            callback(err, 'Error');
        } else {
            callback(err, data);
        }
        db.disconnect();
    });
};

// TODO: create Discussions_Impl specific methods below as needed

module.exports = Discussions_Impl;